<?php
/**
 * inbox.php 收集箱
 * 用户Jack将自己生活中遇到的生词通过该页面添加到自己的账户中，这写单词最初保存在Jack的Inbox内。
 */
require_once 'setting.php';
require_once 'smarty.php';
require_once 'functions.php';
require_once 'paging.php';

require_once INCLUDESPATH . 'db/User.php';
require_once INCLUDESPATH . 'db/UserDAO.php';
require_once INCLUDESPATH . 'db/Word.php';
require_once INCLUDESPATH . 'db/WordDAO.php';

# start the session function
if (!isset($_SESSION)) {
	session_start();
}


# << start - ...
# @todo inbox 验证用户是否已经登陆，未登陆的话跳转到对应的页面，已登陆则继续
if (isset($_SESSION['user'])) {
	$user = $_SESSION['user'];
} else {
	jumpToPage('signin.php?redirect=inbox.php');
}
# >> end - ...

# 提示信息
$tipmessage = '';

# << start - inbox page
if (isset($_POST['words'])) {
	$wordstr = $_POST['words'];
	# 如果输入为空，提示Jack，他需要先添加单词
	if ($wordstr == '') {
		showInboxPage($pagetitle['inbox'], 1, 'Please must at least enter a word!');
	}
	$goodwords = array();
	$badwords = array();
	# 处理Jack输入的单词字符串，整理出合法的单词和不合法的单词
	operateWords($wordstr, &$goodwords, &$badwords);
	# 处理整理后的单词数组
	if (!empty($goodwords)) {
		# add good words to the database
		$worddao = new WordDAO();
		foreach ($goodwords as $key) {
			$word = new Word();
			$word->setUserid($user->getId());
			$word->setKey($key);
			$word->setAddTime(formattime(time()));
			# 判断单词是否在数据库中已经存在
			if (!(wordExist($user->getId(), $key))) {
				$worddao->insert($word);
				# 从goodwords中删除该key并添加到existwords中
				# $existwords[] = $key;
				# @todo delete words in goodwords
			}
		}
		# 显示完成后的页面以及相关的信息
		if (!empty($badwords)) {
			showInboxPage($pagetitle['inbox'], 1, count($goodwords) . ' words "' .
			implode(', ', $goodwords) . '" add successful, and ' . count($badwords) .
			' words "' . implode(', ', $badwords) . '" is invalid!');
		} else {
			showInboxPage($pagetitle['inbox'], 1, count($goodwords) . ' words "' .
			implode(', ', $goodwords) . '" add successful');
		}
	} else {
		showInboxPage($pagetitle['inbox'], 1, 'All the word is invalid, please check your input!');
	}
} elseif (isset($_GET['next']) && $_GET['next'] == 'delete') {
	# @todo 删除单词
	if (isset($_GET['id'])) {
		$id = intval($_GET['id']);
		if ($id < 0) {
			$id = 0;
		}
		$worddao = new WordDAO();
		$word = $worddao->retrieveById($id);
		if ($word) {
			if (wordExist($user->getId(), $word->getKey())) {
				$worddao->delete($id);
				$tipmessage = 'Delete word "' . $word->getKey() . '" successfully.';
			} else {
				$tipmessage = 'An error occur!';
			}
		}
	}
	if (isset($_GET['page'])) {
		$page = intval($_GET['page']);
		if ($page <= 0) {
			$page = 1;
		}
	}
	showInboxPage($pagetitle['inbox'], $page, $tipmessage);
} elseif (isset($_GET['page'])) {
	$page = intval($_GET['page']);
	if ($page <= 0) {
		$page = 1;
	}
	showInboxPage($pagetitle['inbox'], $page, 'You can add a word or words with comma between them!');
}else {
	showInboxPage($pagetitle['inbox'], 1, 'You can add a word or words with comma between them!');
}
# >> end - inbox page


function operateWords($wordstr, &$goodwords, &$badwords) {
	$words = explode(',', $wordstr);
	foreach ($words as $word) {
		$word = trim($word); # trim the word
		if ($word == '') {
			continue;
		} else {
			if (validWord($word)) {
				$goodwords[] = $word;
			} else {
				$badwords[] = $word;
			}
		}
	}
}

function showInboxPage($title, $currentpage = 1, $tipsmsg='') {

	$smarty = new MySmarty();
	$paging = new PagingWords($currentpage, 'inbox');
	# << start - 显示页面
	# 标题
	$smarty->assign('title', $title);
	# 提示信息
	if ($tipsmsg != '') {
		$smarty->assign('tipsmsg', $tipsmsg);
	}
	# 单词列表
	# @todo 显示单词列表
	$smarty->assign('wordslist', $paging->getPagingWords());
	# 分页链接
	$paginglinks = $paging->getPagingLinks();
	foreach ($paginglinks as $key => $value) {
		$smarty->assign($key, $value);
	}
	# 当前页与总页数
	$smarty->assign('currentpage', $paging->getCurrentPage());
	$smarty->assign('pagesamount', $paging->getPagesAmount());

	$smarty->display('inbox.tpl');
	# >> end - 显示页面
}
?>